package com.tensqure.spit.service;

import com.tensqure.spit.dao.SpitDao;
import com.tensqure.spit.pojo.Spit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import util.IdWorker;

import java.util.Date;
import java.util.List;

/**
 * Created by 队长 on 2018/12/7.
 */
@Service
@Transactional
public class SpitService {

    @Autowired
    private SpitDao spitDao;
    @Autowired
    private IdWorker idWorker;
    @Autowired
    private MongoTemplate mongoTemplate;


    /**
     * mongodb 查询所有
     * @return
     */
    public List<Spit> findAll(){
        return spitDao.findAll();
    }

    /**
     * 按id查询mongodb单个
     * @param id
     * @return
     */
    public Spit findById(String id){
        return spitDao.findById(id).get();
    }

    /**
     * 按对象保存数据
     * @param spit
     */
    public void save(Spit spit){
        spit.set_id(idWorker.nextId()+"");
        spit.setPublishtime(new Date());// 发布日期
        spit.setVisits(0);// 浏览量
        spit.setShare(0);// 分享数
        spit.setThumbup(0);// 点赞数
        spit.setComment(0);//回复数
        spit.setState("1");// 状态
        // 如果当前添加的吐槽回复数 加一
        if (spit.getParentId() != null && spit.getParentId() != null){
            Query query = new Query();
            query.addCriteria(Criteria.where("_id").is(spit.getParentId()));
            Update update = new Update();
            update.inc("comment",1);
            mongoTemplate.updateFirst(query,update,"spit");
        }
        spitDao.insert(spit);
    }

    /**
     * 按对象保存数据
     * @param spit
     */
    public void update(Spit spit){
        spitDao.insert(spit);
    }

    /**
     * 按对象保存数据
     * @param id
     */
    public void deleteById(String id){
        spitDao.deleteById(id);
    }

    /**
     * 根据上级id分页
     * @param parentid
     * @param page
     * @param size
     * @return
     */
    public Page<Spit> findByParentId(String parentid, int page, int size ){
        Pageable pageable = PageRequest.of(page-1,size);
        return spitDao.findByParentId(parentid,pageable);
    }
    /**
     * 点赞
     * @param spitId
     */
    public void thumbup(String spitId) {
        //先查询到当前对象
//        Spit spit = spitDao.findById(spitId).get();
//        // 如果点赞为null  先赋值为一
//        spit.setThumbup((spit.getThumbup() == null?0:spit.getThumbup())+1);
//        spitDao.save(spit);
        //方式二 使用原生mongo命令来实现自增
        Query query = new Query();
        query.addCriteria(Criteria.where("_id").is("1071048653449224192"));
        Update update = new Update();
        update.inc("thumbup",1);
        mongoTemplate.updateFirst(query,update,"spit");
    }
}
